package com.aquarium.ui.controller;

import android.R.layout;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.aquarium.R;
import com.aquarium.config.ModuleData;
import com.aquarium.config.WaterMotorData;
import com.aquarium.data.ModuleSettingDataBase;

public class ModuleTimePicker extends Activity {
	protected String mIntentAction;

	protected ModuleSettingDataBase mModuleSettingDataBase;
	protected TimePicker mTimePicker;
	protected LinearLayout mWeekLayout;
	protected Button mOkButton;
	protected Button mCancelButton;
	protected TextView mMonTextView;
	protected TextView mTuesTextView;
	protected TextView mWedTextView;
	protected TextView mThurTextView;
	protected TextView mFriTextView;
	protected TextView mSatTextView;
	protected TextView mSunTextView;

	protected boolean[] mWeekChecked;
	protected boolean[] mWeekCheckedBackup;

	protected ModuleData mModuleData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mModuleSettingDataBase = ModuleSettingDataBase.getInstance(this);
		mWeekChecked = new boolean[7];
		mWeekCheckedBackup = new boolean[7];

		Intent intent = getIntent();
		mIntentAction = intent.getAction();
	}

	public void init() {
		subInit();
		findViews();
		setListeners();
		initCheckedBools();
	}

	protected void subInit(){
		System.out.println("XXXXXXXXOOOXXXXXXXXx");
	}
	public void findViews() {
		mTimePicker = (TimePicker) findViewById(R.id.time_picker);
		mWeekLayout = (LinearLayout) findViewById(R.id.week_pick_layout);
		mOkButton = (Button) findViewById(R.id.ok_btn);
		mCancelButton = (Button) findViewById(R.id.cancel_btn);
		mMonTextView = (TextView) findViewById(R.id.mon_txv);
		mTuesTextView = (TextView) findViewById(R.id.tues_txv);
		mWedTextView = (TextView) findViewById(R.id.wed_txv);
		mThurTextView = (TextView) findViewById(R.id.thur_txv);
		mFriTextView = (TextView) findViewById(R.id.fri_txv);
		mSatTextView = (TextView) findViewById(R.id.sat_txv);
		mSunTextView = (TextView) findViewById(R.id.sun_txv);

		mTimePicker.setIs24HourView(false);
	}

	public void setWidgetValue(ModuleData data) {
		if (!data.isAM) {
			mTimePicker.setCurrentHour(data.hour + 12);
		} else {
			mTimePicker.setCurrentHour(data.hour);
		}
		mTimePicker.setCurrentMinute(data.minute);
		mWeekChecked = new boolean[] { data.isMon, data.isTues, data.isWed,
				data.isThur, data.isFri, data.isSat, data.isSun };
		initWeekCheckedBackup();
		setWeekTextViews(mWeekChecked);
	}

	public void setListeners() {
		mWeekLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				AlertDialog.Builder builder = new AlertDialog.Builder(
						ModuleTimePicker.this);
				builder.setTitle(getResources().getString(R.string.week))
						.setMultiChoiceItems(
								getWeekStrs(),
								mWeekCheckedBackup,
								new DialogInterface.OnMultiChoiceClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which, boolean isChecked) {
										mWeekCheckedBackup[which] = isChecked;
									}
								})
						.setPositiveButton(
								getResources().getString(R.string.ok),
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										setWeekTextViews(mWeekCheckedBackup);
										setWeekChecked();
									}
								})
						.setNegativeButton(
								getResources().getString(R.string.cancel),
								new DialogInterface.OnClickListener() {

									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										initWeekCheckedBackup();
									}
								});
				builder.show();
			}
		});

	}

	private String[] getWeekStrs() {
		String[] weekStrs = new String[7];
		Resources res = getResources();
		weekStrs[0] = res.getString(R.string.full_name_mon);
		weekStrs[1] = res.getString(R.string.full_name_tues);
		weekStrs[2] = res.getString(R.string.full_name_wed);
		weekStrs[3] = res.getString(R.string.full_name_thur);
		weekStrs[4] = res.getString(R.string.full_name_fri);
		weekStrs[5] = res.getString(R.string.full_name_sat);
		weekStrs[6] = res.getString(R.string.full_name_sun);
		return weekStrs;
	}

	private void initCheckedBools() {
		if (mIntentAction.equals(ControllerSetting.ACTION_ADD_SETTING)) {
			for (int i = 0; i < 7; i++) {
				mWeekChecked[i] = true;
				mWeekCheckedBackup[i] = true;
			}
		} else if (mIntentAction
				.equals(ControllerSetting.ACTION_MODIFY_SETTING)) {

		}
	}

	private void initWeekCheckedBackup() {
		for (int i = 0; i < 7; i++) {
			mWeekCheckedBackup[i] = mWeekChecked[i];
		}
	}

	private void setWeekChecked() {
		for (int i = 0; i < 7; i++) {
			mWeekChecked[i] = mWeekCheckedBackup[i];
		}
	}

	private void setWeekTextViews(boolean[] weekChecked) {
		if (weekChecked.length != 7) {
			return;
		}
		mMonTextView.setVisibility(weekChecked[0] == true ? View.VISIBLE
				: View.GONE);
		mTuesTextView.setVisibility(weekChecked[1] == true ? View.VISIBLE
				: View.GONE);
		mWedTextView.setVisibility(weekChecked[2] == true ? View.VISIBLE
				: View.GONE);
		mThurTextView.setVisibility(weekChecked[3] == true ? View.VISIBLE
				: View.GONE);
		mFriTextView.setVisibility(weekChecked[4] == true ? View.VISIBLE
				: View.GONE);
		mSatTextView.setVisibility(weekChecked[5] == true ? View.VISIBLE
				: View.GONE);
		mSunTextView.setVisibility(weekChecked[6] == true ? View.VISIBLE
				: View.GONE);
	}

	public void setModuleData(ModuleData data) {
		int hour = mTimePicker.getCurrentHour();
		if (hour > 12) {
			data.isAM = false;
			data.hour = hour - 12;
		} else {
			data.isAM = true;
			data.hour = hour;
		}
		data.minute = mTimePicker.getCurrentMinute();
		data.isMon = mWeekChecked[0];
		data.isTues = mWeekChecked[1];
		data.isWed = mWeekChecked[2];
		data.isThur = mWeekChecked[3];
		data.isFri = mWeekChecked[4];
		data.isSat = mWeekChecked[5];
		data.isSun = mWeekChecked[6];

	}
}
